package com.example.demo.GPSTest;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.demo.HTTPTest.util.OKHttpUtil;
import okhttp3.OkHttpClient;

import java.util.Map;

public class GPSController {
    public static void main(String[] args) {

        String longitude = "119.87736";
        String latitude = "31.490584";


        try {
            // 示例：查询经纬度（30.1234, 120.5678）的位置
            String addressJson = getAddress(latitude, longitude);
            System.out.println("位置信息：" + addressJson);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String getAddress(String latitude, String longitude) throws Exception {
        String apiKey = "38cf3f575c867559781cbd1fb4879ee0";

        String url = "https://restapi.amap.com/v3/geocode/regeo?" +
                "key=" + apiKey +
                "&location=" + longitude + "," + latitude + // 高德顺序为 lng, lat
                "&output=json";

        Map<String, Object> doget = OKHttpUtil.doget(url);
        if (doget.get("status").toString().equals("200")) {
            JSONObject result = JSONUtil.parseObj(doget.get("result"));
            JSONObject regeocode = result.getJSONObject("regeocode");
            JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
            String province = addressComponent.getStr("province");
            String district = addressComponent.getStr("district");
            String address = province +"·"+district;
            System.out.println(address);
            return address;

        }
        return null;
    }
}
